//
//  FamilyViewCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/20.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol FamilyViewCellDelegate: NSObjectProtocol {
    func familyViewCell(willEdit cell: FamilyViewCell)
    func familyViewCell(willBindCard cell: FamilyViewCell)
}

class FamilyViewCell: UITableViewCell {
    
    weak var delegate: FamilyViewCellDelegate?
    
    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }
        context.setFillColor(UIColor.background.cgColor)
        context.fill(CGRect(x: 0, y: 122, width: screenW, height: 16))
    }
    
    func configure(with vm: FamilyRowViewModel, type: FamilyCellType) {
        nameLabel.text = vm.name
        statusLabel.text = ""
        phoneLabel.text = vm.phone
        bindCardBtn.isHidden = vm.cardStatus != .unbound
        cardLabel.text = vm.cardText
        cardLabel.textColor = vm.cardType.textColor
        editBtn.isHidden = type == .report
    }
    
    @IBOutlet private weak var nameLabel: UILabel!
    
    @IBOutlet private weak var statusLabel: UILabel!
    
    @IBOutlet private weak var phoneLabel: UILabel!
    
    @IBOutlet private weak var cardLabel: UILabel!
    
    @IBOutlet private weak var bindCardBtn: UIButton! {
        didSet {
            bindCardBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            bindCardBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
            bindCardBtn.layer.cornerRadius = bindCardBtn.height / 2
            bindCardBtn.layer.borderWidth = 1
            bindCardBtn.layer.borderColor = UIColor.mainColorLight.cgColor
        }
    }
    
    @IBOutlet private weak var editBtn: UIButton! {
        didSet {
            editBtn.setBackgroundImage(UIImage(color: .white), for: .normal)
            editBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
        }
    }
    
    
    @IBAction private func bindCardAction(_ sender: UIButton) {
        delegate?.familyViewCell(willBindCard: self)
    }
    
    @IBAction private func editBtnClicked(_ sender: UIButton) {
        delegate?.familyViewCell(willEdit: self)
    }
    
}
